package com.openbravo.pos.panels;

import com.openbravo.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.SerializableRead;
import com.openbravo.data.loader.SerializerReadBasic;
import com.openbravo.data.loader.SerializerReadClass;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.format.Formats;
import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.forms.AppProperties;
import com.openbravo.pos.forms.AppView;
import com.openbravo.pos.util.StringUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.table.AbstractTableModel;

public class PaymentsModel
{
  private String m_sHost;
  private int m_iSeq;
  private Date m_dDateStart;
  private Date m_dDateEnd;
  private Integer m_iPayments;
  private Double m_dPaymentsTotal;
  private List<PaymentsLine> m_lpayments;
  private static final String[] PAYMENTHEADERS = { "Label.Payment", "label.totalcash" };
  private Integer m_iSales;
  private Double m_dSalesBase;
  private Double m_dSalesTaxes;
  private List<SalesLine> m_lsales;
  private static final String[] SALEHEADERS = { "label.taxcash", "label.totalcash" };

  public static PaymentsModel emptyInstance()
  {
    PaymentsModel localPaymentsModel = new PaymentsModel();
    localPaymentsModel.m_iPayments = new Integer(0);
    localPaymentsModel.m_dPaymentsTotal = new Double(0.0D);
    localPaymentsModel.m_lpayments = new ArrayList();
    localPaymentsModel.m_iSales = null;
    localPaymentsModel.m_dSalesBase = null;
    localPaymentsModel.m_dSalesTaxes = null;
    localPaymentsModel.m_lsales = new ArrayList();
    return localPaymentsModel;
  }

  public static PaymentsModel loadInstance(AppView paramAppView)
    throws BasicException
  {
    PaymentsModel localPaymentsModel = new PaymentsModel();
    localPaymentsModel.m_sHost = paramAppView.getProperties().getHost();
    localPaymentsModel.m_iSeq = paramAppView.getActiveCashSequence();
    localPaymentsModel.m_dDateStart = paramAppView.getActiveCashDateStart();
    localPaymentsModel.m_dDateEnd = null;
    Object[] arrayOfObject1 = (Object[])(Object[])new StaticSentence(paramAppView.getSession(), "SELECT COUNT(*), SUM(PAYMENTS.TOTAL) FROM PAYMENTS, RECEIPTS WHERE PAYMENTS.RECEIPT = RECEIPTS.ID AND RECEIPTS.MONEY = ?", SerializerWriteString.INSTANCE, new SerializerReadBasic(new Datas[] { Datas.INT, Datas.DOUBLE })).find(paramAppView.getActiveCashIndex());
    if (arrayOfObject1 == null)
    {
      localPaymentsModel.m_iPayments = new Integer(0);
      localPaymentsModel.m_dPaymentsTotal = new Double(0.0D);
    }
    else
    {
      localPaymentsModel.m_iPayments = ((Integer)arrayOfObject1[0]);
      localPaymentsModel.m_dPaymentsTotal = ((Double)arrayOfObject1[1]);
    }
    List localList1 = new StaticSentence(paramAppView.getSession(), "SELECT PAYMENTS.PAYMENT, SUM(PAYMENTS.TOTAL) FROM PAYMENTS, RECEIPTS WHERE PAYMENTS.RECEIPT = RECEIPTS.ID AND RECEIPTS.MONEY = ? GROUP BY PAYMENTS.PAYMENT", SerializerWriteString.INSTANCE, new SerializerReadClass(PaymentsLine.class)).list(paramAppView.getActiveCashIndex());
    if (localList1 == null)
      localPaymentsModel.m_lpayments = new ArrayList();
    else
      localPaymentsModel.m_lpayments = localList1;
    Object[] arrayOfObject2 = (Object[])(Object[])new StaticSentence(paramAppView.getSession(), "SELECT COUNT(DISTINCT RECEIPTS.ID), SUM(TICKETLINES.UNITS * TICKETLINES.PRICE) FROM RECEIPTS, TICKETLINES WHERE RECEIPTS.ID = TICKETLINES.TICKET AND RECEIPTS.MONEY = ?", SerializerWriteString.INSTANCE, new SerializerReadBasic(new Datas[] { Datas.INT, Datas.DOUBLE })).find(paramAppView.getActiveCashIndex());
    if (arrayOfObject2 == null)
    {
      localPaymentsModel.m_iSales = null;
      localPaymentsModel.m_dSalesBase = null;
    }
    else
    {
      localPaymentsModel.m_iSales = ((Integer)arrayOfObject2[0]);
      localPaymentsModel.m_dSalesBase = ((Double)arrayOfObject2[1]);
    }
    Object[] arrayOfObject3 = (Object[])(Object[])new StaticSentence(paramAppView.getSession(), "SELECT SUM(TAXLINES.AMOUNT) FROM RECEIPTS, TAXLINES WHERE RECEIPTS.ID = TAXLINES.RECEIPT AND RECEIPTS.MONEY = ?", SerializerWriteString.INSTANCE, new SerializerReadBasic(new Datas[] { Datas.DOUBLE })).find(paramAppView.getActiveCashIndex());
    if (arrayOfObject3 == null)
      localPaymentsModel.m_dSalesTaxes = null;
    else
      localPaymentsModel.m_dSalesTaxes = ((Double)arrayOfObject3[0]);
    List localList2 = new StaticSentence(paramAppView.getSession(), "SELECT TAXCATEGORIES.NAME, SUM(TAXLINES.AMOUNT) FROM RECEIPTS, TAXLINES, TAXES, TAXCATEGORIES WHERE RECEIPTS.ID = TAXLINES.RECEIPT AND TAXLINES.TAXID = TAXES.ID AND TAXES.CATEGORY = TAXCATEGORIES.ID AND RECEIPTS.MONEY = ?GROUP BY TAXCATEGORIES.NAME", SerializerWriteString.INSTANCE, new SerializerReadClass(SalesLine.class)).list(paramAppView.getActiveCashIndex());
    if (localList2 == null)
      localPaymentsModel.m_lsales = new ArrayList();
    else
      localPaymentsModel.m_lsales = localList2;
    return localPaymentsModel;
  }

  public int getPayments()
  {
    return this.m_iPayments.intValue();
  }

  public double getTotal()
  {
    return this.m_dPaymentsTotal.doubleValue();
  }

  public String getHost()
  {
    return this.m_sHost;
  }

  public int getSequence()
  {
    return this.m_iSeq;
  }

  public Date getDateStart()
  {
    return this.m_dDateStart;
  }

  public void setDateEnd(Date paramDate)
  {
    this.m_dDateEnd = paramDate;
  }

  public Date getDateEnd()
  {
    return this.m_dDateEnd;
  }

  public String printHost()
  {
    return StringUtils.encodeXML(this.m_sHost);
  }

  public String printSequence()
  {
    return Formats.INT.formatValue(Integer.valueOf(this.m_iSeq));
  }

  public String printDateStart()
  {
    return Formats.TIMESTAMP.formatValue(this.m_dDateStart);
  }

  public String printDateEnd()
  {
    return Formats.TIMESTAMP.formatValue(this.m_dDateEnd);
  }

  public String printPayments()
  {
    return Formats.INT.formatValue(this.m_iPayments);
  }

  public String printPaymentsTotal()
  {
    return Formats.CURRENCY.formatValue(this.m_dPaymentsTotal);
  }

  public List<PaymentsLine> getPaymentLines()
  {
    return this.m_lpayments;
  }

  public int getSales()
  {
    return this.m_iSales == null ? 0 : this.m_iSales.intValue();
  }

  public String printSales()
  {
    return Formats.INT.formatValue(this.m_iSales);
  }

  public String printSalesBase()
  {
    return Formats.CURRENCY.formatValue(this.m_dSalesBase);
  }

  public String printSalesTaxes()
  {
    return Formats.CURRENCY.formatValue(this.m_dSalesTaxes);
  }

  public String printSalesTotal()
  {
    return Formats.CURRENCY.formatValue((this.m_dSalesBase == null) || (this.m_dSalesTaxes == null) ? null : Double.valueOf(this.m_dSalesBase.doubleValue() + this.m_dSalesTaxes.doubleValue()));
  }

  public List<SalesLine> getSaleLines()
  {
    return this.m_lsales;
  }

  public AbstractTableModel getPaymentsModel()
  {
    return new AbstractTableModel()
    {
      public String getColumnName(int paramInt)
      {
        return AppLocal.getIntString(PaymentsModel.PAYMENTHEADERS[paramInt]);
      }

      public int getRowCount()
      {
        return PaymentsModel.this.m_lpayments.size();
      }

      public int getColumnCount()
      {
        return PaymentsModel.PAYMENTHEADERS.length;
      }

      public Object getValueAt(int paramInt1, int paramInt2)
      {
        PaymentsModel.PaymentsLine localPaymentsLine = (PaymentsModel.PaymentsLine)PaymentsModel.this.m_lpayments.get(paramInt1);
        switch (paramInt2)
        {
        case 0:
          return localPaymentsLine.getType();
        case 1:
          return localPaymentsLine.getValue();
        }
        return null;
      }
    };
  }

  public AbstractTableModel getSalesModel()
  {
    return new AbstractTableModel()
    {
      public String getColumnName(int paramInt)
      {
        return AppLocal.getIntString(PaymentsModel.SALEHEADERS[paramInt]);
      }

      public int getRowCount()
      {
        return PaymentsModel.this.m_lsales.size();
      }

      public int getColumnCount()
      {
        return PaymentsModel.SALEHEADERS.length;
      }

      public Object getValueAt(int paramInt1, int paramInt2)
      {
        PaymentsModel.SalesLine localSalesLine = (PaymentsModel.SalesLine)PaymentsModel.this.m_lsales.get(paramInt1);
        switch (paramInt2)
        {
        case 0:
          return localSalesLine.getTaxName();
        case 1:
          return localSalesLine.getTaxes();
        }
        return null;
      }
    };
  }

  public static class PaymentsLine
    implements SerializableRead
  {
    private String m_PaymentType;
    private Double m_PaymentValue;

    public void readValues(DataRead paramDataRead)
      throws BasicException
    {
      this.m_PaymentType = paramDataRead.getString(1);
      this.m_PaymentValue = paramDataRead.getDouble(2);
    }

    public String printType()
    {
      return AppLocal.getIntString("transpayment." + this.m_PaymentType);
    }

    public String getType()
    {
      return this.m_PaymentType;
    }

    public String printValue()
    {
      return Formats.CURRENCY.formatValue(this.m_PaymentValue);
    }

    public Double getValue()
    {
      return this.m_PaymentValue;
    }
  }

  public static class SalesLine
    implements SerializableRead
  {
    private String m_SalesTaxName;
    private Double m_SalesTaxes;

    public void readValues(DataRead paramDataRead)
      throws BasicException
    {
      this.m_SalesTaxName = paramDataRead.getString(1);
      this.m_SalesTaxes = paramDataRead.getDouble(2);
    }

    public String printTaxName()
    {
      return this.m_SalesTaxName;
    }

    public String printTaxes()
    {
      return Formats.CURRENCY.formatValue(this.m_SalesTaxes);
    }

    public String getTaxName()
    {
      return this.m_SalesTaxName;
    }

    public Double getTaxes()
    {
      return this.m_SalesTaxes;
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.panels.PaymentsModel
 * JD-Core Version:    0.6.0
 */